//具有亲缘关系 的进程 共享内存
//先mmap  再FORK
//  功能验证： 子进程写   父进程读取共享内存的内容 ，


# include <stdio.h>
# include <stdlib.h>
# include <sys/mman.h>
#include <string. h>

#define MEMSIZE  1024

int main (){
    char * ptr;
    pid_t pid;

    ptr = mmap (NULL, MEMSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,-1,0) //fd -1 匿名映射

    if(ptr == MAP FAILED){
        perror ("mmap ()");
        exit(1);
    }

    
    pid = fork(); 
    if (pid < 0){
         perror("fork() "); 
         munmap(ptr, MEMSIZE);   //创建失败，需要释放父进程的ptr
         exit (1);
    }
    if(pid == 0){   //子进程  写
        strcpy(ptr, "Hello!"); 
        munmap (ptr, MEMSIZE);
        exit (0);
    }  
    else{          //父进程  读
        wait (NULL);
        puts (ptr); 
        munmap (ptr, MEMSIZE);
        exit (O);
    }
    exit (0);
}





